home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / XPBITMAP.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  14KB  |  327 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XPBITMAP
  3. ;
  4. ; Planar Bitmap functions - System Ram <-> Video Ram
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ; ****** Aeronautical Research Laboratory              ****************
  14. ; ****** Defence Science and Technology Organisation   ****************
  15. ; ****** Australia                                     ****************
  16. ;
  17. ; egg@dstos3.dsto.gov.au
  18. ; teg@bart.dsto.gov.au
  19. ;-----------------------------------------------------------------------
  20.  
  21.  
  22. COMMENT $
  23.  
  24.   This module implements a set of functions to operate on planar bitmaps.
  25.   Planar bitmaps as used by these functions have the following structure:
  26.  
  27.   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255
  28.   BYTE 1                 The bitmap height in rows range 1..255
  29.   BYTE 2..n1             The plane 0 pixels width*height bytes
  30.   BYTE n1..n2            The plane 1 pixels width*height bytes
  31.   BYTE n2..n3            The plane 2 pixels width*height bytes
  32.   BYTE n3..n4            The plane 3 pixels width*height bytes
  33.  
  34.   These functions provide the fastest possible bitmap blts from system ram to
  35.   to video and further, the single bitmap is applicable to all pixel
  36.   allignments. The masked functions do not need separate masks since all non
  37.   zero pixels are considered to be masking pixels, hence if a pixel is 0 the
  38.   corresponding screen destination pixel is left unchanged.
  39.  
  40.  
  41. $
  42.  
  43. include xlib.inc
  44. include xpbitmap.inc
  45.  
  46.  
  47. MOVSB_OP equ 0A4h
  48.  
  49.     .code
  50.  
  51. ;----------------------------------------------------------------------
  52. ; x_put_masked_pbm - mask write a planar bitmap from system ram to video ram
  53. ; all zero source bitmap bytes indicate destination byte to be left unchanged
  54. ;
  55. ; Source Bitmap structure:
  56. ;
  57. ;  Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
  58. ;  Bitmap data (plane 2)..,Bitmap data (plane 3)..
  59. ;
  60. ;  note width is in bytes ie lots of 4 pixels
  61. ;
  62. ;  x_put_masked_pbm(X,Y,ScrnOffs,char far * Bitmap)
  63. ;
  64. ;
  65. ; LIMITATIONS: No clipping is supported
  66. ;              Only supports bitmaps with widths which are a multiple of
  67. ;              4 pixels
  68. ;
  69. ; Written by Themie Gouthas
  70. ;----------------------------------------------------------------------
  71. _x_put_masked_pbm  proc
  72.         ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  73.     LOCAL Plane:byte,BMHeight:byte,NextLineIncr:word=LocalStk
  74.     push  bp
  75.         mov   bp,sp
  76.     sub   sp,LocalStk                 ; Create space for local variables
  77.         push  si
  78.         push  di
  79.     push  ds
  80.     cld
  81.         mov   ax,SCREEN_SEG
  82.         mov   es,ax
  83.         mov   ax,[Y]                      ; Calculate dest screen row
  84.     mov   bx,[_ScrnLogicalByteWidth]  ;  by mult. dest Y coord by Screen
  85.     mul   bx                          ;  width then adding screen offset
  86.         mov   di,[ScrnOffs]               ;  store result in DI
  87.         add   di,ax
  88.         mov   cx,[X]                      ; Load X coord into CX and make a
  89.     mov   dx,cx                       ;  copy in DX
  90.     shr   dx,2                        ; Find starting byte in dest row
  91.     add   di,dx                       ;  add to DI giving screen offset of
  92.                                           ;  first pixel's byte
  93.         lds   si,[Bitmap]                 ; DS:SI -> Bitmap data
  94.         lodsw                             ; Al = B.M. width (bytes) AH = B.M.
  95.                                           ;  height
  96.     mov   [BMHeight],ah               ; Save source bitmap dimensions
  97.         xor   ah,ah                       ; NextLineIncr = bytes to the begin.
  98.     sub   bx,ax                       ;  of bitmaps next row on screen
  99.     mov   [NextLineIncr],bx           ;
  100.     mov   bh,al                       ; Use bh as column loop count
  101.         and   cx,0003h                    ; mask X coord giving plane of 1st
  102.                       ; bitmap pixel(zero CH coincidentally)
  103.     mov   ah,11h                      ; Init. mask for VGA plane selection
  104.         shl   ah,cl                       ; Shift for starting pixel plane
  105.         mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  106.         mov   al,MAP_MASK
  107.         out   dx,al
  108.         inc   dx
  109.     mov   [Plane],4                   ; Set plane counter to 4
  110. @@PlaneLoop:
  111.         push  di                          ; Save bitmap's start dest. offset
  112.         mov   bl,[BMHeight]               ; Reset row counter (BL)
  113.         mov   al,ah
  114.         out   dx,al                       ; set vga write plane
  115. @@RowLoop:
  116.     mov   cl,bh                       ; Reset Column counter cl
  117. @@ColLoop:
  118.         lodsb                             ; Get next source bitmap byte
  119.         or    al,al                       ; If not zero then write to dest.
  120.         jz    @@NoPixel                   ; otherwise skip to next byte
  121.         mov   es:[di],al
  122. @@NoPixel:
  123.         inc   di
  124.         loop  @@ColLoop                   ; loop if more columns left
  125.         add   di,[NextLineIncr]           ; Move to next row
  126.         dec   bl                          ; decrement row counter
  127.         jnz   @@RowLoop                   ; Jump if more rows left
  128.         pop   di                          ; Restore bitmaps start dest byte
  129.         rol   ah,1                        ; Shift mask for next plane
  130.     adc   di,0                        ; If wrapped increment dest address
  131.     dec   [Plane]                     ; Decrement plane counter
  132.         jnz   @@PlaneLoop                 ; Jump if more planes left
  133.  
  134.         pop   ds                          ; restore data segment
  135.         pop   di                          ; restore registers
  136.         pop   si
  137.         mov   sp,bp                       ; dealloc local variables
  138.         pop   bp
  139.         ret
  140. _x_put_masked_pbm  endp
  141.  
  142.  
  143. ;----------------------------------------------------------------------
  144. ; x_put_pbm - Write a planar bitmap from system ram to video ram
  145. ;
  146. ; Source Bitmap structure:
  147. ;
  148. ;  Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
  149. ;  Bitmap data (plane 2)..,Bitmap data (plane 3)..
  150. ;
  151. ;  note width is in bytes ie lots of 4 pixels
  152. ;
  153. ;  x_put_pbm(X,Y,ScrnOffs,char far * Bitmap)
  154. ;
  155. ;
  156. ; LIMITATIONS: No clipping is supported
  157. ;              Only supports bitmaps with widths which are a multiple of
  158. ;              4 pixels
  159. ; FEATURES   : Automatically selects REP MOVSB or REP MOVSW  depending on
  160. ;              source bitmap width, by modifying opcode ;-).
  161. ;
  162. ; Written by Themie Gouthas
  163. ;----------------------------------------------------------------------
  164. _x_put_pbm  proc
  165.     ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  166.     LOCAL Plane:byte,BMHeight:byte,NextLineIncr:word=LocalStk
  167.     push  bp
  168.         mov   bp,sp
  169.     sub   sp,LocalStk                 ; Create space for local variables
  170.     push  si
  171.         push  di
  172.     push  ds
  173.     cld
  174.         mov   ax,SCREEN_SEG
  175.         mov   es,ax
  176.         mov   ax,[Y]                      ; Calculate dest screen row
  177.     mov   bx,[_ScrnLogicalByteWidth]  ;  by mult. dest Y coord by Screen
  178.     mul   bx                          ;  width then adding screen offset
  179.         mov   di,[ScrnOffs]               ;  store result in DI
  180.         add   di,ax
  181.         mov   cx,[X]                      ; Load X coord into CX and make a
  182.     mov   dx,cx                       ;  copy in DX
  183.     shr   dx,2                        ; Find starting byte in dest row
  184.     add   di,dx                       ;  add to DI giving screen offset of
  185.                                           ;  first pixel's byte
  186.         lds   si,[Bitmap]                 ; DS:SI -> Bitmap data
  187.         lodsw                             ; Al = B.M. width (bytes) AH = B.M.
  188.                                           ;  height
  189.         mov   [BMHeight],ah               ; Save source bitmap dimensions
  190.         xor   ah,ah                       ; NextLineIncr = bytes to the begin.
  191.     sub   bx,ax                       ;  of bitmaps next row on screen
  192.     mov   [NextLineIncr],bx           ;
  193.     mov   bh,al
  194.                       ; Self Modifying, Shame, shame shame..
  195.         and   cx,0003h                    ; mask X coord giving plane of 1st
  196.                                           ; bitmap pixel(zero CH coincidentally)
  197.     mov   ah,11h                      ; Init. mask for VGA plane selection
  198.         shl   ah,cl                       ; Shift for starting pixel plane
  199.         mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  200.         mov   al,MAP_MASK
  201.         out   dx,al
  202.         inc   dx
  203.     mov   [Plane],4                   ; Set plane counter to 4
  204. @@PlaneLoop:
  205.         push  di
  206.     mov   bl,[BMHeight]
  207.         mov   al,ah
  208.         out   dx,al
  209. @@RowLoop:
  210.     mov   cl,bh
  211. @@REP_OP:
  212.         rep   movsb                       ; Copy a complete row for curr plane
  213.         add   di,[NextLineIncr]           ; Move to next row
  214.         dec   bl                          ; decrement row counter
  215.         jnz   @@RowLoop                   ; Jump if more rows left
  216.         pop   di                          ; Restore bitmaps start dest byte
  217.         rol   ah,1                        ; Shift mask for next plane
  218.     adc   di,0                        ; If wrapped increment dest address
  219.     dec   [Plane]                     ; Decrement plane counter
  220.         jnz   @@PlaneLoop                 ; Jump if more planes left
  221.  
  222.         pop   ds                          ; restore data segment
  223.         pop   di                          ; restore registers
  224.         pop   si
  225.         mov   sp,bp                       ; dealloc local variables
  226.         pop   bp
  227.         ret
  228. _x_put_pbm  endp
  229.  
  230. ;----------------------------------------------------------------------
  231. ; x_get_pbm - Read a planar bitmap to system ram from video ram
  232. ;
  233. ; Source Bitmap structure:
  234. ;
  235. ;  Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
  236. ;  Bitmap data (plane 2)..,Bitmap data (plane 3)..
  237. ;
  238. ;  note width is in bytes ie lots of 4 pixels
  239. ;
  240. ;  x_get_pbm(X,Y,BMwidth,BMheight,ScrnOffs,char far * Bitmap)
  241. ;
  242. ;
  243. ; LIMITATIONS: No clipping is supported
  244. ;              Only supports bitmaps with widths which are a multiple of
  245. ;              4 pixels
  246. ; FEATURES   : Automatically selects REP MOVSB or REP MOVSW  depending on
  247. ;              source bitmap width, by modifying opcode ;-).
  248. ;
  249. ; Written by Themie Gouthas
  250. ;----------------------------------------------------------------------
  251. _x_get_pbm  proc
  252.     ARG X:word,Y:word,SrcWidth:byte,SrcHeight:byte,ScrnOffs:word,Bitmap:dword
  253.     LOCAL Plane:byte,NextLineIncr:word=LocalStk
  254.     push  bp
  255.         mov   bp,sp
  256.     sub   sp,LocalStk                 ; Create space for local variables
  257.         push  si
  258.         push  di
  259.         push  ds
  260.     cld
  261.  
  262.     mov   ax,[Y]                      ; Calculate screen row
  263.     mov   bx,[_ScrnLogicalByteWidth]  ;  by mult. Y coord by Screen
  264.     mul   bx                          ;  width then adding screen offset
  265.     mov   si,[ScrnOffs]               ;  store result in SI
  266.     add   si,ax
  267.         mov   cx,[X]                      ; Load X coord into CX and make a
  268.     mov   dx,cx                       ;  copy in DX
  269.     shr   dx,2                        ; Find starting byte in screen row
  270.     add   si,dx                       ;  add to SI giving screen offset of
  271.                       ;  first pixel's byte
  272.     mov   ax,SCREEN_SEG
  273.     mov   ds,ax
  274.     les   di,[Bitmap]                 ; ES:DI -> Bitmap data
  275.     mov   al,[SrcWidth]
  276.     mov   ah,[SrcHeight]
  277.     stosw                             ; Al = B.M. width (bytes) AH = B.M.
  278.                                           ;  height
  279.         xor   ah,ah                       ; NextLineIncr = bytes to the begin.
  280.     sub   bx,ax                       ;  of bitmaps next row on screen
  281.     mov   [NextLineIncr],bx           ;
  282.     mov   bh,MOVSB_OP
  283.     mov   bh,al
  284.                       ; Self Modifying, Shame, shame shame..
  285.         and   cx,0003h                    ; mask X coord giving plane of 1st
  286.                                           ; bitmap pixel(zero CH coincidentally)
  287.     mov   ah,11h                      ; Init. mask for VGA plane selection
  288.     shl   ah,cl                       ; Shift for starting pixel plane
  289.     mov   dx,GC_INDEX                 ; Prepare VGA for cpu to video reads
  290.     mov   al,READ_MAP
  291.         out   dx,al
  292.         inc   dx
  293.     mov   [Plane],4                   ; Set plane counter (BH) to 4
  294.     mov   al,cl
  295. @@PlaneLoop:
  296.     push  si
  297.     mov   bl,[SrcHeight]
  298.         out   dx,al
  299. @@RowLoop:
  300.     mov   cl,bh
  301. @@REP_OP:
  302.     rep   movsb                       ; Copy a complete row for curr plane
  303.     add   si,[NextLineIncr]           ; Move to next row
  304.         dec   bl                          ; decrement row counter
  305.         jnz   @@RowLoop                   ; Jump if more rows left
  306.     pop   si                          ; Restore bitmaps start dest byte
  307.  
  308.     inc   al                          ; Select next plane to read from
  309.     and   al,3                        ;
  310.  
  311.     rol   ah,1                        ; Shift mask for next plane
  312.     adc   si,0                        ; If wrapped increment dest address
  313.     dec   [Plane]                     ; Decrement plane counter
  314.     jnz   @@PlaneLoop                 ; Jump if more planes left
  315.  
  316.         pop   ds                          ; restore data segment
  317.         pop   di                          ; restore registers
  318.         pop   si
  319.         mov   sp,bp                       ; dealloc local variables
  320.         pop   bp
  321.         ret
  322. _x_get_pbm  endp
  323.  
  324.  
  325.     end
  326.  
  327.